const axios = require("axios");

const instance = axios.create({
  baseURL: "https://sale.joinf.com/api",
  headers: {
    Authorization:
      "Bearer TRsdRerescHHqEt2KJ6TQccZ6WsyAt6uu5XtZRDXPKxmgp8a0JD7SjifDhzKZi2g",
    accessToken:
      "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI2NDM3IiwiZXhwIjoxNzE3MDQ4OTc4LCJpYXQiOjE3MTY5NjI1Nzh9._hd-_AxUktuUwZajGJoWfBYJfAVZ4KuDaqdynJIpZPQ",
  },
});

instance.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

module.exports = function (month) {
  const today = new Date();
  const year = today.getFullYear();
  month = month !== undefined ? month : today.getMonth() + 1;
  const dateStr = `${year}-${("0" + month).slice(-2)}`;

  let resolve;
  const promise = new Promise((res) => {
    resolve = res;
  });

  instance({
    method: "post",
    url: "/attendance/record/selectList",
    data: {
      year,
      month,
      dateStr,
      employeeIds: 6437,
      currenPage: 1,
      pageRows: 40,
      pagingRequired: true,
    },
  })
    .then((res) => {
      resolve(res.data || []);
    })
    .catch(() => {
      resolve([]);
    });

    return promise;
};
